/**  
 * All rights Reserved, Designed By www.tydic.com
 * @Title:  TestServiceImpl.java   
 * @Package com.axin.service.impl   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: axin     
 * @date:   2019年3月10日 下午1:55:59   
 * @version V1.0 
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
package com.axin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.axin.common.entity.ApiToken;
import com.axin.common.entity.DataGridResult;
import com.axin.common.entity.JsonResult;
import com.axin.common.entity.PageObject;
import com.axin.common.entity.response.ApiResponse;
import com.axin.common.entity.response.Currency;
import com.axin.common.entity.response.CurrencysResponse;
import com.axin.common.entity.response.Symbol;
import com.axin.common.entity.response.TimestampResponse;
import com.axin.common.exception.ParamIllegalException;
import com.axin.common.huobi.HttpRequest;
import com.axin.common.utils.ListPageHelper;
import com.axin.dao.HbCurrencysMapper;
import com.axin.dao.HbSymbolsMapper;
import com.axin.entity.HbCurrencys;
import com.axin.entity.HbSymbols;
import com.axin.entity.example.HbCurrencysExample;
import com.axin.entity.example.HbSymbolsExample;
import com.axin.entity.example.HbSymbolsExample.Criteria;
import com.axin.service.CommonService;
import com.fasterxml.jackson.core.type.TypeReference;

/**   
 * @Description: TODO 
 * @ClassName:  TestServiceImpl
 * @author:  Axin 
 * @date:   2019年3月10日 下午1:55:59   
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
@Service
public class CommonServiceImpl implements CommonService{

	
	@Autowired
	private HttpRequest httpRequest;
	
	@Autowired
	private HbSymbolsMapper symbolsMapper;
	
	@Autowired
	private HbCurrencysMapper currencysMapper;
	
	/**获取所有交易对url*/
	@Value("${API_COMMON_SYMBOLS_URL}")
	private String API_COMMON_SYMBOLS_URL;
	@Value("${API_COMMON_CURRENCYS_URL}")
	private String API_COMMON_CURRENCYS_URL;

	/**查询系统当前时间url*/
	@Value("${API_COMMON_TIMESTAMP_URL}")
	private String API_COMMON_TIMESTAMP_URL;
	
	/* (non-Javadoc)
	 * @see com.axin.service.CommonService#importRemoteSymbols(com.axin.common.entity.ApiToken)
	 */
	@Override
	public JsonResult importRemoteSymbols(ApiToken token) {
		ApiResponse<List<Symbol>> resp =
				 httpRequest.get(token,
						 		API_COMMON_SYMBOLS_URL,
						 		null,
						 		new TypeReference<ApiResponse<List<Symbol>>>() {
	                			});
		 
		 List<Symbol> list = resp.checkAndReturn();
		 
		 //删除现有数据
		 HbSymbolsExample example = new HbSymbolsExample();
		 symbolsMapper.deleteByExample(example);
		 
		 for (Symbol symbol : list) {
			 HbSymbols hbSymbols = new HbSymbols();
			 hbSymbols.setBaseCurrency(symbol.getBaseCurrency());
			 hbSymbols.setQuoteCurrency(symbol.getQuoteCurrency());
			 hbSymbols.setAmountPrecision(symbol.getAmountPrecision());
			 hbSymbols.setPricePrecision(symbol.getPricePrecision());
			 hbSymbols.setSymbolPartition(symbol.getSymbolPartition());
			 hbSymbols.setSymbol(symbol.getSymbol());
			 symbolsMapper.insert(hbSymbols);
		}
		 
		 return new JsonResult();
		 
		
	}
	
	

	/* (non-Javadoc)
	 * @see com.axin.service.CommonService#importRemoteCurrencys(com.axin.common.entity.ApiToken)
	 */
	@Override
	public JsonResult importRemoteCurrencys(ApiToken token) {
		 CurrencysResponse resp = httpRequest.get(token,API_COMMON_CURRENCYS_URL, null, new TypeReference<CurrencysResponse>() {
	        });
		  if("ok".equals(resp.getStatus())){
	        	List<String> list = resp.getData();
	        	if(CollectionUtils.isNotEmpty(list)){
	        		//删除现有数据
	       		 HbCurrencysExample example = new HbCurrencysExample();
	       		 this.currencysMapper.deleteByExample(example);
	       		 
	        		for (String string : list) {
	        			HbCurrencys currency = new HbCurrencys();
	        			currency.setName(string);
	        			this.currencysMapper.insert(currency);
					}
	        	}
	        }
		return new JsonResult();
	}



	/* (non-Javadoc)
	 * @see com.axin.service.CommonService#getSymbols(com.axin.entity.request.ApiToken)
	 */
	@Override
	public DataGridResult getSymbols(ApiToken token,PageObject page) {
		 ApiResponse<List<Symbol>> resp =
				 httpRequest.get(token,
						 		API_COMMON_SYMBOLS_URL,
						 		null,
						 		new TypeReference<ApiResponse<List<Symbol>>>() {
	                			});
		 
		 List<Symbol> list = resp.checkAndReturn();
		 int size = list.size();
		 if(page != null){
			 ListPageHelper<Symbol> pageHelper = new ListPageHelper<>();
			 try {
				list = pageHelper.page(page.getPage(), page.getRows(), list);
			} catch (Exception e) {
				e.printStackTrace();
			}
		 }
		 
		 return new DataGridResult(size,list);
	}

	/* (non-Javadoc)
	 * @see com.axin.service.CommonService#getCurrencys(com.axin.entity.request.ApiToken, com.axin.common.entity.PageObject)
	 */
	@Override
	public DataGridResult getCurrencys(ApiToken token, PageObject page) {
	        CurrencysResponse resp = httpRequest.get(token,API_COMMON_CURRENCYS_URL, null, new TypeReference<CurrencysResponse>() {
	        });
	        if("ok".equals(resp.getStatus())){
	        	List<String> list = resp.getData();
	        	if(CollectionUtils.isNotEmpty(list)){
	        		List<Currency> newList = new ArrayList<>();
	        		for (String str : list) {
	        			newList.add(new Currency(str));
					}
	        		int size = newList.size();
	        		if(page != null){
	        			 ListPageHelper<Currency> pageHelper = new ListPageHelper<>();
	        			 try {
	        				 newList = pageHelper.page(page.getPage(), page.getRows(), newList);
	        			} catch (Exception e) {
	        				e.printStackTrace();
	        			}
	        		}
	        		return new DataGridResult(size,newList);
	        	}
	        }
	        return null;
	}



	/* (non-Javadoc)
	 * @see com.axin.service.CommonService#selectLocalSymbols(com.axin.entity.HbSymbols, com.axin.common.entity.PageObject)
	 */
	@Override
	public DataGridResult selectLocalSymbols(HbSymbols symbols, PageObject page) {
			
		HbSymbolsExample example = new HbSymbolsExample();
		Criteria criteria = example.createCriteria();
		if(StringUtils.isNotEmpty(symbols.getSymbol())){
			criteria.andSymbolEqualTo(symbols.getSymbol());
		}
		if(StringUtils.isNotEmpty(symbols.getSymbolPartition())){
			criteria.andSymbolPartitionEqualTo(symbols.getSymbolPartition());
		}
		example.setOrderByClause("symbol");
		List<HbSymbols> list = this.symbolsMapper.selectByParam(symbols);
		
		int size = list.size();
		ListPageHelper<HbSymbols> pageHelper = new ListPageHelper<>();
		 try {
			list = pageHelper.page(page.getPage(), page.getRows(), list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return new DataGridResult(size,list);
	}

	

	/* (non-Javadoc)
	 * @see com.axin.service.CommonService#selectLocalSymbols()
	 */
	@Override
	public List<HbSymbols> selectLocalSymbols() {
		HbSymbolsExample example = new HbSymbolsExample();
		List<HbSymbols> list = this.symbolsMapper.selectByExample(example);
		return list;
	}
	
	



	/* (non-Javadoc)
	 * @see com.axin.service.CommonService#selectLocalSymbolsByLikeSymbol(java.lang.String)
	 */
	@Override
	public List<HbSymbols> selectLocalSymbolsByLikeSymbol(String q) {
		return this.symbolsMapper.selectByLikeSymbol(q);
	}



	/* (non-Javadoc)
	 * @see com.axin.service.CommonService#selectLocalSymbolsByParam(com.axin.entity.HbSymbols)
	 */
	@Override
	public HbSymbols selectLocalSymbolsByParam(HbSymbols symbols) {
		if(StringUtils.isEmpty(symbols.getSymbol())
				&& StringUtils.isEmpty(symbols.getBaseCurrency())
				&& StringUtils.isEmpty(symbols.getQuoteCurrency())){
			throw new ParamIllegalException("param is null");
		}
		HbSymbolsExample example = new HbSymbolsExample();
		Criteria criteria = example.createCriteria();
		if(StringUtils.isNotEmpty(symbols.getSymbol())){
			criteria.andSymbolEqualTo(symbols.getSymbol());
		}
		if(StringUtils.isNotEmpty(symbols.getBaseCurrency())){
			criteria.andBaseCurrencyEqualTo(symbols.getBaseCurrency());
		}
		if(StringUtils.isNotEmpty(symbols.getQuoteCurrency())){
			criteria.andQuoteCurrencyEqualTo(symbols.getQuoteCurrency());
		}
		List<HbSymbols> list = this.symbolsMapper.selectByExample(example);
		if(CollectionUtils.isNotEmpty(list)){
			return list.get(0);
		}
		return null;
	}


	

	/* (non-Javadoc)
	 * @see com.axin.service.CommonService#selectLocalSymbolsByName(java.lang.String)
	 */
	@Override
	public HbSymbols selectLocalSymbolsBySymbol(String symbol) {
		HbSymbols symbols = new HbSymbols();
		symbols.setSymbol(symbol);
		return selectLocalSymbolsByParam(symbols);
	}



	/* (non-Javadoc)
	 * @see com.axin.service.CommonService#selectLocalCurrencys(java.lang.String, com.axin.common.entity.PageObject)
	 */
	@Override
	public DataGridResult selectLocalCurrencys(String param, PageObject page) {
		HbCurrencysExample example = new HbCurrencysExample();
		com.axin.entity.example.HbCurrencysExample.Criteria criteria = example.createCriteria();
		if(StringUtils.isNotEmpty(param)){
			criteria.andNameEqualTo(param);
		}
		example.setOrderByClause("name");
		List<HbCurrencys> list = this.currencysMapper.selectByParam(param);
		int size = list.size();
		ListPageHelper<HbCurrencys> pageHelper = new ListPageHelper<>();
		 try {
			list = pageHelper.page(page.getPage(), page.getRows(), list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return new DataGridResult(size,list);
	}



	/* (non-Javadoc)
	 * @see com.axin.service.CommonService#timestamp()
	 */
	@Override
	public TimestampResponse timestamp(ApiToken token) {
		 TimestampResponse resp = httpRequest.get(token,API_COMMON_TIMESTAMP_URL, null, new TypeReference<TimestampResponse>() {
	        });
	        return resp;
	}



	/* (non-Javadoc)
	 * @see com.axin.service.CommonService#getDateTime(com.axin.common.entity.ApiToken)
	 */
	@Override
	public String getDateTime(ApiToken token) {
		TimestampResponse timestamp = timestamp(token);
		String string = new DateTime(timestamp.getData()).toString("yyyy-MM-dd HH:mm:ss");
		return string;
	}

	
	
	
	
	

	
	
}
